#include "ccamera.h"
#include "cmatrix.h"
#include <GL/glu.h>

CCamera::CCamera()
{
    this->m_Position	= CPoint3f (0.0f,0.0f,0.0f);
    this->m_Direction	= CVector3f (0.0f, 0.0f, 1.0f);
    this->m_Up		= CVector3f (0.0f, 1.0f, 0.0f);
    this->m_Side		= CVector3f (1.0f, 0.0f, 0.0f);
    this->m_Target	= this->m_Position +  this->m_Direction;
    this->m_fPhi = 0.0;
    this->m_fTheta = 0.0;
    this->m_uWindow_height = 0;
    this->m_uWindow_width = 0;
    this->m_fFovy = 0.0f;
}


void CCamera::VectorsFromAngles()
{
    CMatrix Xrot(1.0, 0.0, 0.0, 0.0, cos(m_fTheta), -sin(m_fTheta), 0.0, sin(m_fTheta), cos(m_fTheta));
    CMatrix	Yrot(cos(m_fPhi), 0.0, sin(m_fPhi), 0.0, 1.0, 0.0, -sin(m_fPhi), 0.0, cos(m_fPhi));
    this->m_fPhi = 0.0;
    this->m_fTheta = 0.0;
    CMatrix transformMat(Yrot*Xrot);

    this->m_Direction   = transformMat * this->m_Direction;
    this->m_Up          = transformMat * this->m_Up;;
    this->m_Side		= transformMat * this->m_Side;


    this->m_Target = this->m_Position +  this->m_Direction;
}

void CCamera::look()
{
    gluLookAt(this->m_Position.GetX(),this->m_Position.GetY(),this->m_Position.GetZ(),
              this->m_Target.GetX(), this->m_Target.GetY(), this->m_Target.GetZ(),
              this->m_Up.m_fX,this->m_Up.m_fY,this->m_Up.m_fZ);
}


float CCamera::getRatio()
{
    if (this->m_uWindow_height >= 0.0)
        return (float)this->m_uWindow_width/(float)this->m_uWindow_height;
    return 0.0;
}

void CCamera::normalizeVectors()
{
    this->m_Direction.normalize();
    this->m_Up.normalize();
    this->m_Side.normalize();
}
